//
//  STLReader.m
//  iDent
//
//  Created by Marius Corega on 7/21/09.
//  Copyright 2009 Marius Corega. All rights reserved.
//

#import "STLReader.h"
#include <stdlib.h>
#import "STLElement.h"

@implementation STLReader

-(void) loadData:(NSData *) data
{
	[data getBytes:&header length:80];
	[data getBytes:&nr_points range:NSMakeRange(80, 4)];
	
	_vertex = [[NSMutableArray alloc] init];
	
	for (int i=0; i<nr_points; i++) 
	{
		TSTLElement v;
		int dim = 50;
		
		[data getBytes:&v range:NSMakeRange(84+dim*i, dim)];
		
		//NSData *d = [[data subdataWithRange:NSMakeRange(84+dim*i, dim)] autorelease];
		
		//STLElement *e = [[STLElement alloc] initWithData:d];
		STLElement *e = [[STLElement alloc] init];

		//[e retain];
		[e setElement:v];
		
		[_vertex addObject:e];
		
	}
	
}


-(id) initWithData:(NSData *) data
{
	[super init];
	
	[self loadData:data];
	
	return self;
}


+(id) stlReaderFromFile:(NSString *) fname
{
	
	NSData *data = [NSData dataWithContentsOfFile:fname];
	self = [[super alloc] initWithData:data ] ;
	
//	[self loadData:fdata];
	
	return self;
}

-(NSMutableArray*) vertex
{
	return  _vertex;
}

-(unsigned long) count
{
	return nr_points;
}

-(void) dealloc
{
	[_vertex removeAllObjects];
	[_vertex release];
	_vertex = nil;
	
	[super dealloc];
}

@end
